<template>
    <div class="goodsinfo"> 
        <!-- 头部图片区 -->               
        <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
          <div class="ball" v-show="ballflag" ref="ball"></div>
        </transition>
        <div class="mui-card">
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
                         <swiper :swipelist="swipelist"  :flag="false"></swiper>
					</div>
				</div>
		</div>

    <!-- 中间内容区 -->    
        <div class="mui-card">
				<div class="mui-card-header">{{goodsinfo.title}}</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<p class="price">
                            市场价:&nbsp;￥{{goodsinfo.market_price}} &nbsp;&nbsp;销售价:<span class="saleprice">{{goodsinfo.sell_price}}</span>
                        </p>
                        <p>
                            购买数量:<number @getCount="getGoodsNumber" :max="goodsinfo.stock_quantity"></number>
                        </p>
                        <p>
                    <mt-button type="primary">立即购买</mt-button>
                    <mt-button type="danger" @click="addShopCar">加入购物车</mt-button>
                        </p>
					</div>
				</div>
		</div>
         <!--底部参数区  -->
        <div class="mui-card">
                    <div class="mui-card-header">商品参数</div>
                    <div class="mui-card-content">
                        <div class="mui-card-content-inner">
                            <p>商品货号:{{goodsinfo.goods_no}}</p>
                            <p>库存情况:{{goodsinfo.stock_quantity}}件</p>
                            <p>上架时间:{{goodsinfo.add_time | dataFormat}}</p>
                       </div>
                    </div>
                    <div class="mui-card btn">
                    <mt-button type="primary" size="large" plain>图文介绍</mt-button>
                    <br>
                    <mt-button type="danger"  size="large" plain>商品评论</mt-button>
                    </div>
        </div>		
    </div>
</template>
<script>
import swiper from '../swiper/swiper.vue' 
import number from '../swiper/number.vue' 

export default {
    data(){
        return {
            id:this.$route.params.id,
            swipelist:[],
            ballflag: false,
            goodsinfo:{},
            goodsnumber:1
        }
    },
    methods:{
        getgoodsinfo(){
            this.$http.get('api/goods/getinfo/'+this.id).then(result=>{
                if(result.body.status===0){
                    this.goodsinfo = result.body.message[0];
                    // console.log(result.body.message);
                }else{
                    alert( "请求失败")
                }
            })
        },
        getGoodsImg(){
            this.$http.get('api/getthumimages/'+this.id).then(result=>{
                if(result.body.status===0){
                    // console.log(result.body.message)
                    this.swipelist = result.body.message;
                    this.swipelist.forEach(element => {
                        element.img = element.src
                    });
                  
                }else{
                    alert("请求失败")
                }
            })
        },
        addShopCar(){
            // 添加至购物车
           this.ballflag = !this.ballflag;
        //    console.log("父级组件获取到的数量为"+this.goodsnumber)
        var shopInfo = {
            id:this.id,
            number:parseInt(this.goodsnumber),
            price:this.goodsinfo.sell_price,
            seleted:true
        }
            this.$store.commit('addCar',shopInfo);
        },
        beforeEnter(el){
            el.style.transform="translate(0,0)"
        },
        enter(el,done){
             el.offsetHeight 
            var ballPosition = this.$refs.ball.getBoundingClientRect() //获取圆球的方位
            // 购物车的位置
            var badgePosition  = document.getElementById('badge').getBoundingClientRect();
        
            var x = badgePosition.left - ballPosition.left;
            var y = badgePosition.top - ballPosition.top;
            el.style.transform=`translate(${x}px,${y}px)`
            el.style.transition = "all .5s cubic-bezier(.53,.8,.79,.79)"
            done();
        },
        afterEnter(el){
            this.ballflag = !this.ballflag;
        },
        getGoodsNumber(count){
            this.goodsnumber = count;
        }
       
    },
    created(){
        this.getgoodsinfo();
        this.getGoodsImg();
       
    },
    components:{
        swiper,
        number
    },
}
</script>
<style lang="scss" scoped>

 .goodsinfo{
     padding-bottom: 70px;
     .mui-card-header{
         font-size: 18px;
     }
     .mui-card-content-inner{
         p{
             font-size: 14px;
         }
     }
      .ball{
       width: 15px;
       height: 15px;
       border-radius: 50%;
       background: red;
       position: absolute;
       z-index: 9991;
       left:135px;
       top:405px;    
   }
 }
</style>

